package com.example.demo.service;

import com.example.demo.entity.Line;
import com.example.demo.mapper.LineMapper;
import com.example.demo.tool.AppHttpCodeEnum;
import com.example.demo.tool.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName LineServiceImpl
 * @Description TODO
 * @Author FZY
 * @Date 2023/9/20 22:22
 * @Version 1.0
 */
@Service
public class LineServiceImpl implements LineService{

    @Autowired
    private LineMapper lineMapper;


    @Override
    public ResponseResult<Line> save(Line line) {
        try {
            lineMapper.insert(line);

        }catch (RuntimeException e){
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);

        }
        return ResponseResult.okResult("OK");
    }

    @Override
    public ResponseResult<Line> deleteById(Integer id) {
        try {
            lineMapper.deleteById(id);

        }catch (RuntimeException e){
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);

        }
        return ResponseResult.okResult("OK");
    }

    @Override
    public ResponseResult<Line> getById(Integer id) {
        Line line;
        try {
            line = lineMapper.selectById(id);

        } catch (RuntimeException e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);

        }
        return ResponseResult.okResult(line);
    }

    @Override
    public ResponseResult<Line> updateById(Line line) {
        try {
            lineMapper.updateById(line);

        }catch (RuntimeException e){
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);

        }
        return ResponseResult.okResult("OK");
    }

    @Override
    public ResponseResult<List<Line>> getLines() {
        List<Line> list;
        try {
            list = lineMapper.selectList(null);

        } catch (RuntimeException e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);

        }
        return ResponseResult.okResult(list);
    }

}
